#include "keypad.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QtCore>
#include <QGraphicsEffect>
#include <QTreeView>
#include <QRectF>

Keypad::Keypad()
{
    visible = false;
    selected = 0;

    img_path = new QString(QApplication::applicationDirPath());

    keypad_img = new QImage(QString("%1/Images/keypad.png").arg(*img_path));
    minimize_icon = new QImage(QString("%1/Images/minimize_icon.png").arg(*img_path));
    button_border = new QImage(QString("%1/Images/keypad_border.png").arg(*img_path));
    setRect(0, 0, keypad_img->width(), keypad_img->height());
    setFlag(ItemIsMovable);

    button1 = new QRectF(20, 50, 37, 30);
    button2 = new QRectF(63, 50, 37, 30);
    button3 = new QRectF(105, 50, 37, 30);
    button4 = new QRectF(20, 106, 37, 30);
    button5 = new QRectF(63, 106, 37, 30);
    button6 = new QRectF(105, 106, 37, 30);
    button7 = new QRectF(20, 162, 37, 30);
    button8 = new QRectF(63, 162, 37, 30);
    button9 = new QRectF(105, 162, 37, 30);

    selected_button = new QRectF(0,0,0,0);

    itemScale = (float) QApplication::desktop()->height() / (float)1080;
    this->setScale(itemScale/4);
    this->setPos(900,50);

    DropShadow = new QGraphicsDropShadowEffect();
    DropShadow->setOffset(5,5);
    DropShadow->setEnabled(false);
    DropShadow->setBlurRadius(6);
    this->setGraphicsEffect(DropShadow);
}

void Keypad::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->drawImage( option->exposedRect, *keypad_img, option->exposedRect, Qt::OrderedAlphaDither );

    if(visible)
    p->drawImage((this->rect().width()- minimize_icon->width()), 0 , *minimize_icon);
    if(selected != 0)
    p->drawImage(*selected_button, *button_border);
}

void Keypad::changeStatus()
{
    visible = !visible;

    if(visible){
        QPropertyAnimation *anim = new QPropertyAnimation(this, "scale");
        anim->setEasingCurve(QEasingCurve::OutCubic);
        anim->setDuration(500);
        anim->setStartValue(itemScale/4);
        anim->setEndValue(itemScale);
        anim->start();
        DropShadow->setEnabled(true);
    }
    else{
        QPropertyAnimation *anim = new QPropertyAnimation(this, "scale");
        anim->setEasingCurve(QEasingCurve::OutCubic);
        anim->setDuration(500);
        anim->setStartValue(itemScale);
        anim->setEndValue(itemScale/4);
        anim->start();
        DropShadow->setEnabled(false);
    }
}

void Keypad::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(visible){
        if(button1->contains(event->pos())){
            if(event->button() == Qt::RightButton){
                selected = 1; selected_button = button1; }
            if(event->button() == Qt::LeftButton)
                emit tecla1Clicked();
        }
        if(button2->contains(event->pos())){
            if(event->button() == Qt::RightButton){
                selected = 2; selected_button = button2; }
            if(event->button() == Qt::LeftButton)
                emit tecla2Clicked();
        }
        if(button3->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 3; selected_button = button3; }
            if(event->button() == Qt::LeftButton)
                emit tecla3Clicked();
        }
        if(button4->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 4; selected_button = button4; }
            if(event->button() == Qt::LeftButton)
                emit tecla4Clicked();
        }
        if(button5->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 5; selected_button = button5; }
            if(event->button() == Qt::LeftButton)
                emit tecla5Clicked();
        }
        if(button6->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 6; selected_button = button6; }
            if(event->button() == Qt::LeftButton)
                emit tecla6Clicked();
        }
        if(button7->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 7; selected_button = button7; }
            if(event->button() == Qt::LeftButton )
                emit tecla7Clicked();
        }
        if(button8->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 8; selected_button = button8; }
            if(event->button() == Qt::LeftButton)
                emit tecla8Clicked();
        }
        if(button9->contains(event->pos())){
            if(event->button() == Qt::RightButton) {
                selected = 9; selected_button = button9; }
            if(event->button() == Qt::LeftButton)
                emit tecla9Clicked();
        }
    }

    QGraphicsRectItem::mousePressEvent(event);
    this->update();
}
void Keypad::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(!visible)
        changeStatus();
    else if(QRectF(this->rect().width()- minimize_icon->width(), 0,
              minimize_icon->width(), minimize_icon->height()).contains(event->pos())){
        changeStatus();
    }
}

int Keypad::isSelected()
{
    return selected;
}
